

import h3d.col.HeightMap;
import hxmath.math.Vector2;
import hxd.Math;
import hxmath.math.MathUtil;
import json2object.JsonParser;
import tool.PixelTool;
import hxd.Pixels;
import hxd.BitmapData;
import haxe.io.Int32Array;
import format.agal.Data.Reg;
import view.Root_View;
import data.Data_Canvas;
import tree.Tress;
import hl.Type;
import view.View_Main;
import hxd.Timer;
import hxd.Rand;
import udprotean.shared.Utils;
import h2d.Camera;
import h2d.col.Point;
import h2d.Flow;
import h3d.Vector;
import hxd.fmt.grd.Data.Color;
import sdl.Window;
import h2d.Bitmap;
import hxd.res.Image;
import format.abc.Data.Init;
import Xml.XmlType;
import cdb.Data;
import hxd.File;
import tree.Root;
import format.agal.Data.Tex;
import h2d.Font;
import h2d.Text;
import h2d.Object;
import hxd.App;

enum A {
    rgb(r:Int, g:Int, b:Int);
    B;
    C;
    D;
}

class Test {
    public function new() {
        trace(Math.floor(1.1));
        trace(Math.ceil(1.1));

    }
}


class Main extends App {
    public static var instance(default, null) : Main;
    public static var Height = 720;
    public static var Width = 1280;
    public static var H_W = cast(Height, Float) / Width;
    public static function GetInstance() {
        return instance;
    }

    public var UIRoot : Object;
    public var node : Dynamic;
    private var root_follow : Object;//跟随相机的根物体

    
	static function main() {
        hxd.res.Resource.LIVE_UPDATE = true;
        Res.initLocal();
        instance = new Main();
        
        new Test();
	}


    override function onResize() {
        super.onResize();

        var window = hxd.Window.getInstance();
        var sw = window.width / cast(Main.Width, Float);
        var sh = window.height / cast(Main.Height, Float);
        var s = Math.min(sw, sh);
        UIRoot.scaleX = s;
        UIRoot.scaleY = s;
        var x = 0.0;
        var y = 0.0;
        if(sh > sw){
            y = window.height - window.width * H_W;
            y *= 0.5;
        }
        else {
            x = window.width - window.height / H_W;
            x *= 0.5;
        }
        UIRoot.setPosition(x, y);
    }

    var view : View_Main;

    override function init() {
        super.init();
        trace("init");
        root_follow = new Object(s2d);
        UIRoot = new Object(root_follow);
        onResize();
        Root.instance.Start();
        var root_view : Root_View = cast Root.instance.GetLeaf(RView);
        root_view.PushLeaf(VMain);
        hxd.Window.getInstance().addEventTarget((e)->{
            var clone = e.Clone();
            // var pos = ConvertPos(clone.relX, clone.relY);
            // trace(pos);
            // clone.relX = pos.x;
            // clone.relY = pos.y; 
            Root.instance.AddMsg(WindowEvent(clone));
             
        });
    }

    function ConvertPos(x:Float, y:Float) {
        var w = hxd.Window.getInstance().width;
        var h = hxd.Window.getInstance().height;
        var offset_w = 0.;
        var offset_h = 0.;
        var t = h / w;
        if(t > H_W){
            offset_h = h - w * H_W;
        }
        else {
            offset_w = w - h / H_W;
        }
        var tw = Math.inverseLerp(offset_w / 2, w - offset_w / 2, x);
        var th = Math.inverseLerp(offset_h / 2, h - offset_h / 2, y);
        tw = Math.lerp(0.0, Width, tw);
        th = Math.lerp(0.0, Height, th);
        return new Vector2(tw, th);
    }    
   
    
    override function update(dt:Float) {
        super.update(dt);
        Root.instance.Update(dt);
    }


   
}



